package com.shumm.service.impl;

import com.shumm.common.Result;
import com.shumm.mapper.ClassicFormulaCompositionMapper;
import com.shumm.po.ClassicFormulaComposition;
import com.shumm.po.Traditionalchinesemedicine;
import com.shumm.service.ClassicFormulaCompositionService;
import com.shumm.vo.CfcAndTcmAndCf;
import com.shumm.vo.TbAndCfVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassicFormulaCompositionServiceImpl implements ClassicFormulaCompositionService {
    @Autowired
    private ClassicFormulaCompositionMapper mapper;


    @Override
    public Result add(ClassicFormulaComposition cfc) {
        int i = 1 / 0;
        return null;
//        mapper.add(cfc);
//        String message = "添加成功";
//        return Result.success(message, null);
    }

    @Override
    public Result delete(int id) {
        int existed = mapper.isExistedById(id);
        String message = "处方组成已经删除,请更新页面";
        if (existed > 0) {
            mapper.delete(id);
            message = "删除成功";
        }
        return Result.success(message, null);
    }

    @Override
    public Result update(ClassicFormulaComposition cfc) {
//        int i = 1/0;
//        return null;
        int update = mapper.update(cfc);
        String message = "";
        if (update != 0) {
            message = "更新成功";
            return Result.success(message);
        } else {
            message = "更新失败";
            return Result.error(message);
        }
    }

    @Override
    public Result selectById(int id) {
        List<ClassicFormulaComposition> list = mapper.selectById(id);
        return Result.success(list);
    }

    @Override
    public Result selectAll() {
        //        int i = 1/0;
//        return null;
        List<CfcAndTcmAndCf> classicFormulaCompositions = mapper.selectAll();
        System.out.println(classicFormulaCompositions.toString());
        return Result.success(classicFormulaCompositions);
    }

    @Override
    public Result selectByCfId(int cfId) {
        List<ClassicFormulaComposition> classicFormulaCompositions = mapper.selectByCfId(cfId);
        return Result.success(classicFormulaCompositions);
    }

    @Override
    public Result selectByTcmId(int tcmId) {
        List<ClassicFormulaComposition> classicFormulaCompositions = mapper.selectByTcmId(tcmId);
        return Result.success(classicFormulaCompositions);
    }

    @Override
    public Result selectTbByCf(int id) {
        List<TbAndCfVO> shangHanTiaoBians = mapper.selectTbByCf(id);
        return Result.success(shangHanTiaoBians);
    }

    @Override
    public Result selectTcm() {
        List<Traditionalchinesemedicine> tcms = mapper.selectTcm();
        return Result.success(tcms);
    }
}
